package keter.sec.springsec.config;

import keter.sec.springsec.security.encoder.SHAEncoder;
import keter.sec.springsec.security.handler.AjaxFailureHandler;
import keter.sec.springsec.security.handler.FailureHandler;
import keter.sec.springsec.security.handler.SuccessHandler;
import keter.sec.springsec.service.MyUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;

@EnableWebSecurity
@Profile({"sqlite"})
public class BeansConfig {

	@Bean
	public UserDetailsService userDetails() {
	     return new MyUserDetailsService();
    }
	
	@Bean
	public SuccessHandler successHandler() {
		return new SuccessHandler();
	}

	@Bean
	public FailureHandler failuerHandler() {
		return new FailureHandler();
	}

	@Bean
	public AjaxFailureHandler ajaxFailureHandler() {
		return new AjaxFailureHandler();
	}

    @Bean
    public AuthenticationProvider authProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetails());
//        authProvider.setPasswordEncoder(new BCryptEncoder());
        authProvider.setPasswordEncoder(new SHAEncoder());
        return authProvider;
    }

}
